package com.jenkins.test.utils;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0.0
 * @ClassName: {@link HttpClient}
 * @Description: HttpClient - 通用https和http协议
 * @author: Lanzhou
 * @date: 2022/1/21 17:36
 * @Copyright:2020 All rights reserved.
 */
@Slf4j
public class HttpClient {

    /**
     * 超时设置
     */
    private static final long TIME_OUT = 1500L;

    /**
     * OkHttp 优雅, 写法简单啦
     */
    private static final OkHttpClient CLIENT = new OkHttpClient()
            .newBuilder()
            .readTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
            .build();


    /**
     * Get 请求
     *
     * @param url
     * @return
     */
    public static String doGet(String url, Map<String, String> headers) {
        Request.Builder builder = buildHeaderRequest(headers);
        Request request = builder.url(url).get().build();
        return doExecute(request);
    }

    /**
     * Post 请求
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, String json, Map<String, String> headers) {
        //请求参数
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(json));
        Request.Builder builder = buildHeaderRequest(headers);
        Request request = builder.url(url)
                .post(requestBody)
                .build();
        return doExecute(request);
    }

    /**
     * Put 请求
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPut(String url, String json, Map<String, String> headers) {
        //请求参数
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                String.valueOf(json));
        Request.Builder builder = buildHeaderRequest(headers);
        Request request = builder.url(url)
                .put(requestBody)
                .build();
        return doExecute(request);
    }

    /**
     * Delete 请求
     *
     * @param url
     * @return
     */
    public static String doDelete(String url, Map<String, String> headers) {
        //请求参数
        Request.Builder builder = buildHeaderRequest(headers);
        Request request = builder.url(url)
                .delete()
                .build();
        return doExecute(request);
    }


    /**
     * 执行请求
     *
     * @param request
     * @return
     */
    private static String doExecute(Request request) {
        final Call call = CLIENT.newCall(request);
        String result = null;
        try {
            Response response = call.execute();
            result = String.valueOf(response.body());
        } catch (IOException e) {
            log.error("请求执行出错", e);
            result = e.getMessage();
        }
        return result;
    }

    /**
     * 构建头部请求构造器
     *
     * @param headers
     * @return
     */
    private static Request.Builder buildHeaderRequest(Map<String, String> headers) {
        Request.Builder builder = new Request.Builder();
        Optional.ofNullable(headers).ifPresent(hs -> {
            for (Map.Entry<String, String> entry : hs.entrySet()) {
                builder.addHeader(entry.getKey(), entry.getValue());
            }
        });
        return builder;
    }


}
